import { createRouter, createWebHistory } from 'vue-router'
import Login from '@/views/Login/index.vue'
import sHome from '@/views/sHome/index.vue'
import ipintro from '/src/views/sHome/components/InPressureExperiment/ipintro.vue' 
import ipexp from '/src/views/sHome/components/InPressureExperiment/ipexp.vue' 
import exintro from '/src/views/sHome/components/ExPressureExperiment/exintro.vue' 
import exexp from '/src/views/sHome/components/ExPressureExperiment/exexp.vue' 
import blintro from '/src/views/sHome/components/BlastExperiment/blintro.vue' 
import blexp from '/src/views/sHome/components/BlastExperiment/blexp.vue' 
import feedback from '/src/views/sHome/components/FeedBack/feedback.vue'
import communication from '/src/views/sHome/components/Communication/communication.vue'
import usercenter from '/src/views/sHome/components/UserCenter/usercenter.vue'
import homepage from '/src/views/sHome/components/HomePage/homepage.vue'
// import vedio from '/src/views/sHome/components/Vedio/vedio.vue'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component:Login,
      meta: { breadcrumb: '登录' },
    },
    // {
    //   path: '/vedio',
    //   name: 'vedio',
    //   component:vedio,
    //   meta: { breadcrumb: '登录' },
    // },
    {
      path: '/',
      name:'sHome',
      component:sHome,
      children: [
            {
              path: '',
              name: 'homepage',
              component:homepage,
              meta: { breadcrumb: '数字孪生实验室-主页' }
            },
            {
              path: 'ipintro',
              name: 'ipintro',
              component:ipintro,
              meta: { breadcrumb: '数字孪生实验室-内压实验-实验介绍' }
            },
            {
              path: 'ipexp',
              name: 'ipexp',
              component:ipexp,
              meta: { breadcrumb: '数字孪生实验室-内压实验-实验面板' }
            },
            {
              path: 'exintro',
              name: 'exintro',
              component:exintro,
              meta: { breadcrumb: '数字孪生实验室-外压实验-实验介绍' }
            },
            {
              path: 'exexp',
              name: 'exexp',
              component:exexp,
              meta: { breadcrumb: '数字孪生实验室-外压实验-实验面板' }
            },
            {
              path: 'blintro',
              name: 'blintro',
              component:blintro,
              meta: { breadcrumb: '数字孪生实验室-爆破实验-实验介绍' }
            },
            {
              path: 'blexp',
              name: 'blexp',
              component:blexp,
              meta: { breadcrumb: '数字孪生实验室-爆破实验-实验面板' }
            },
            {
              path: 'feedback',
              name: 'feedback',
              component:feedback,
              meta: { breadcrumb: '数字孪生实验室-实验报告提交' }
            },
            {
              path: 'communication',
              name: 'communication',
              component:communication,
              meta: { breadcrumb: '数字孪生实验室-开发与交流' }
            },
            {
              path: 'usercenter',
              name: 'usercenter',
              component:usercenter,
              meta: { breadcrumb: '数字孪生实验室-个人中心' }
            }
      ]
    }
  ]
})
//路由拦截
// router.beforeEach((to, from, next) => {
//   //判断是否是登录保护路由
//   if (to.matched.some(record => record.meta.requiresAuth)) {
//       //验证浏览器本地存储是否有token（或从vuex、Cookie、localStorage中获取，依据自己的存储），用于判断是否登录
//       if (!sessionStorage.token) {//没有token，表示未登录，则重定向到login
//           next({
//               //这里是你登录页面的路由名称，或直接使用 path:'/login'
//               name: 'Login',
//               query: { redirect: to.fullPath }
//           })
//       } else {
//           next()
//       }
//   } else {
//       // 确保一定要调用 next()
//       next()
//   }
// })
router.beforeEach((to,from,next)=>{
  if(to.path==='/login') return next();
  //获取名字为名字为token的token令牌
  const token = window.sessionStorage.getItem('stuNo')
  //不存在则直接跳转到登录界面
  if(!token) return next('/login')
  //否则直接跳转回主页
  next()
});

// router.beforeEach((to, from, next) => {
//   if (from.path === '/exexp') {
//     // 使用replace方法在进入新页面时刷新页面
//     next({ path: to.path, replace: true });
//   } else {
//     next();
//   }
// });

router.afterEach((to, from, next) => {
  if(from.path==='/blexp'&& to.path!=='/blexp'){
    window.location.reload();
  }
  if(from.path==='/exexp'&& to.path!=='/exexp'){
    window.location.reload();
  }
  if(from.path==='/ipexp'&& to.path!=='/ipexp'){
    window.location.reload();
  }
  // console.log( to.path, from.path)
  next();
});          

export default router
